package view;

import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import controller.Login_c;
import controller.Register_c;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.SystemColor;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.net.URL;

public class LoginRegist {

    static boolean flag = true;
    private JFrame frame;
    private JTextField textField;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;
    private JPasswordField passwordField_2;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        try {
            UIManager.put("RootPane.setupButtonVisible", false);
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.translucencySmallShadow;
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        } catch (Exception e) {
            //TODO exception
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginRegist window = new LoginRegist();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public LoginRegist() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */


    private void initialize() {


        //主窗口
        frame = new JFrame();
        //frame.setAlwaysOnTop(true);
        frame.getContentPane().setBackground(UIManager.getColor("CheckBox.background"));
        frame.getContentPane().setLayout(null);

        //第一层panel
        JPanel panel = new JPanel();
        panel.addMouseListener(new MouseAdapter() {
            @Override
            //鼠标单击时获取焦点
            public void mouseClicked(MouseEvent e) {
                panel.grabFocus();
            }
        });
        panel.setFocusable(true);
        panel.setBounds(0, 0, 553, 610);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        //第二层panel，注册框的显示
        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new TitledBorder(null, "注册账号", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_1.setBounds(25, 304, 499, 282);
        panel.add(panel_1);
        panel_1.setLayout(null);

        //账号注册框
        textField_1 = new JTextField();
        textField_1.setText(" 账号（8-15位字母或数字）");
        textField_1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                textField_1.setForeground(Color.black);
                textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            }
        });
        textField_1.addFocusListener(new FocusAdapter() {
            @Override
            //失去焦点时，恢复提示信息
            public void focusLost(FocusEvent e) {
                if (textField_1.getText().isEmpty()){
                    textField_1.setForeground(Color.LIGHT_GRAY);
                    textField_1.setText(" 账号（8-15位字母或数字）");
                }else {
                    if(!textField_1.getText().matches("^[a-zA-Z0-9]{8,15}$")){
                        textField_1.setForeground(Color.red);
                        textField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
                        textField_1.setText(" 格式错误！");
                    }
                }
            }
            @Override
            //获得焦点时，清空提示信息
            public void focusGained(FocusEvent e) {
                if (" 账号（8-15位字母或数字）".equals(textField_1.getText())||" 格式错误！".equals(textField_1.getText())){
                    textField_1.setText("");
                }
            }
        });
        textField_1.setForeground(Color.LIGHT_GRAY);
        textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        textField_1.setBounds(30, 45, 220, 34);
        panel_1.add(textField_1);
        textField_1.setColumns(10);

        //立即注册按钮
        JButton btnNewButton = new JButton("立即注册");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int i = Register_c.regist(textField_1.getText(),String.valueOf(passwordField_1.getPassword()),String.valueOf(passwordField_2.getPassword()));
                if(i==0){
                    JOptionPane.showMessageDialog(frame, "注册成功，请登录！", "提示", JOptionPane.INFORMATION_MESSAGE);
                    frame.setSize(562, 335);
                }else if(i==1){
                    JOptionPane.showMessageDialog(frame, "账号已存在！", "警告", JOptionPane.ERROR_MESSAGE);
                }else if(i==2){
                    JOptionPane.showMessageDialog(frame, "两次密码不同，请重新输入！", "警告", JOptionPane.ERROR_MESSAGE);
                }else if(i==3){
                    JOptionPane.showMessageDialog(frame, "账号为空！", "警告", JOptionPane.ERROR_MESSAGE);
                }else if(i==4){
                    JOptionPane.showMessageDialog(frame, "密码为空！", "警告", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        btnNewButton.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue));
        btnNewButton.setBackground(SystemColor.activeCaption);
        btnNewButton.setForeground(Color.WHITE);
        btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 18));
        btnNewButton.setBounds(30, 202, 216, 35);
        panel_1.add(btnNewButton);

        //欢迎文字
        JLabel lblorangQq = new JLabel("欢迎使用Orang QQ ！");
        lblorangQq.setFont(new Font("微软雅黑", Font.BOLD, 18));
        lblorangQq.setBounds(281, 47, 191, 35);
        panel_1.add(lblorangQq);

        //注册密码框
        passwordField_1 = new JPasswordField();
        passwordField_1.setForeground(Color.LIGHT_GRAY);
        passwordField_1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        passwordField_1.addFocusListener(new FocusAdapter() {
            @Override
            //获取焦点时，将密码框清空，回显字符设为●
            public void focusGained(FocusEvent e) {
                passwordField_1.setForeground(Color.black);
                if (" 输入密码（不少于10位）".equals(new String(passwordField_1.getPassword()))||
                        " 格式错误！".equals(new String(passwordField_1.getPassword()))) {
                    passwordField_1.setText("");
                    passwordField_1.setEchoChar('●');
                }
            }
            @Override
            //失去焦点时，内容设为提示内容
            public void focusLost(FocusEvent e) {
                if (new String(passwordField_1.getPassword()).isEmpty()) {
                    passwordField_1.setForeground(Color.LIGHT_GRAY);
                    char c = 0;
                    passwordField_1.setEchoChar(c);//因为是密码框，设置回显字符为0，会显示明文
                    passwordField_1.setText(" 输入密码（不少于10位）");
                }else if(!new String(passwordField_1.getPassword()).matches("^[a-zA-Z0-9]{0,20}$")){
                    char c = 0;
                    passwordField_1.setEchoChar(c);
                    passwordField_1.setForeground(Color.red);
                    passwordField_1.setText(" 格式错误！");
                }
            }
        });
        char c = 0;
        passwordField_1.setEchoChar(c);
        passwordField_1.setText(" 输入密码（不少于10位）");
        passwordField_1.setBounds(30, 96, 220, 34);
        panel_1.add(passwordField_1);

        //注册确认密码框
        passwordField_2 = new JPasswordField();
        passwordField_2.setForeground(Color.LIGHT_GRAY);
        passwordField_2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        passwordField_2.addFocusListener(new FocusAdapter() {
            @Override
            //获取焦点时，将密码框清空，回显字符设为●
            public void focusGained(FocusEvent e) {
                passwordField_2.setForeground(Color.black);
                if (" 确认密码".equals(new String(passwordField_2.getPassword())))
                    passwordField_2.setText("");
                passwordField_2.setEchoChar('●');
            }
            @Override
            //失去焦点时，内容设为提示内容
            public void focusLost(FocusEvent e) {
                if ("".equals(new String(passwordField_2.getPassword()))) {
                    passwordField_2.setForeground(Color.LIGHT_GRAY);
                    char c = 0;
                    passwordField_2.setEchoChar(c);//因为是密码框，设置回显字符为0，会显示明文
                    passwordField_2.setText(" 确认密码");
                }
            }
        });
        char c2 = 0;
        passwordField_2.setEchoChar(c2);
        passwordField_2.setText(" 确认密码");
        passwordField_2.setBounds(30, 146, 216, 34);
        panel_1.add(passwordField_2);

        JLabel label_1 = new JLabel("");
        ClassLoader cl2 = this.getClass().getClassLoader();
        URL iconURL2 = cl2.getResource("img/企鹅.png");
        label_1.setIcon(new ImageIcon(iconURL2));
        label_1.setBounds(321, 92, 112, 130);
        panel_1.add(label_1);


        JLabel label = new JLabel("");
        ClassLoader cl1 = this.getClass().getClassLoader();
        URL iconURL1 = cl1.getResource("img/桔子.png");
        label.setIcon(new ImageIcon(iconURL1));
        label.setBounds(87, 0, 112, 130);
        panel.add(label);

        JLabel lblOrangeQq = new JLabel("Orange QQ");
        lblOrangeQq.setHorizontalAlignment(SwingConstants.CENTER);
        lblOrangeQq.setFont(new Font("微软雅黑", Font.PLAIN, 35));
        lblOrangeQq.setBounds(206, 37, 229, 72);
        panel.add(lblOrangeQq);

        textField = new JTextField();
        textField.setText(" 账号/邮箱/手机号");
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                textField.setForeground(Color.black);
                textField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
            }
        });
        textField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if (textField.getText().isEmpty()) {
                    textField.setForeground(Color.LIGHT_GRAY);
                    textField.setText(" 账号/邮箱/手机号");
                }
            }

            @Override
            public void focusGained(FocusEvent e) {
                if (" 账号/邮箱/手机号".equals(textField.getText())) {
                    textField.setText("");
                }
            }
        });
        textField.setForeground(Color.LIGHT_GRAY);

        textField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        textField.setBounds(162, 141, 220, 34);
        panel.add(textField);
        textField.setColumns(10);

        JButton button = new JButton("登录");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(Login_c.login(textField.getText(),new String(passwordField_1.getPassword()))){
                    JOptionPane.showMessageDialog(frame, "登录成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                }else {
                    JOptionPane.showMessageDialog(frame, "登录失败，请检查账号、密码！", "警告", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        button.setForeground(Color.WHITE);
        button.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
        button.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button.setBounds(282, 238, 100, 34);
        panel.add(button);

        JButton button_1 = new JButton("注册");
        button_1.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));
        button_1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (flag) {
                    frame.setSize(562, 644);
                } else {
                    frame.setSize(562, 335);
                }
                flag = !flag;
            }
        });
        button_1.setForeground(Color.WHITE);
        button_1.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button_1.setBounds(162, 238, 100, 34);
        panel.add(button_1);

        passwordField = new JPasswordField();
        passwordField.setForeground(Color.LIGHT_GRAY);
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        passwordField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                passwordField.setForeground(Color.black);
                if (" 密码".equals(new String(passwordField.getPassword())))
                    passwordField.setText("");
                passwordField.setEchoChar('●');
            }

            @Override
            public void focusLost(FocusEvent e) {
                if ("".equals(new String(passwordField.getPassword()))) {
                    passwordField.setForeground(Color.LIGHT_GRAY);
                    char c = 0;
                    passwordField.setEchoChar(c);
                    passwordField.setText(" 密码");
                }
            }
        });
        char c1 = 0;
        passwordField.setEchoChar(c1);
        passwordField.setText(" 密码");
        passwordField.setBounds(162, 184, 220, 34);
        panel.add(passwordField);

        frame.setResizable(false);
        frame.setBounds(679, 248, 562, 335);
        //frame.setSize(562, 335);//设置好宽高
        //frame.setLocationRelativeTo(null);//窗体居中显示
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
